package server_commands;

import java.util.StringTokenizer;

import utils.Constants;

/**
 * Klasa predstavlja fabriku za kreiranje komandi. U zavisnosti od prosledjenog parametra
 * konstruktoru, kreira se odgovarajuca potklasa klase Command, koja reprezentuje
 * jednu komandu i za cije je procesuiranje zaduzena. 
 * 
 * @author Uros Krkic
 */
public class CommandFactory implements Constants {

	
	/**
	 * Staticki metod koji parsira komandu. Na osnovu stringa koji predstavlja
	 * komandu (i njen parametar), vrsi se parsiranje ovih vrednosti, i na osnovu
	 * toga se keira odgovarajuca komanda.
	 * @param commandString - string koji sadrzi ime komande i eventualno njen
	 * parametar
	 * @return - kreirana komanda
	 * @author Uros Krkic
	 */
	public static Command createCommand(String commandString) {
		
		if (commandString == null)
			return null;
		
		Command command;
		String cmd = "";
		String parameter = null;
		StringBuffer tempParameter = new StringBuffer();
		int code = -1;
		
		StringTokenizer tokens = new StringTokenizer(commandString);
		
		if (tokens.hasMoreTokens()) {
			cmd = tokens.nextToken();
		}
		while (tokens.hasMoreTokens()) {
			tempParameter.append(tokens.nextToken() + " ");
		}
		if ( tempParameter.length() != 0) {
			int spIndex = tempParameter.lastIndexOf(" ");
			tempParameter.deleteCharAt(spIndex);
			parameter = tempParameter.toString();
		}
		
		for (int i = 0; i < COMMAND_NAMES.length; i++) {
			if ( cmd.toUpperCase().equals(COMMAND_NAMES[i]) ) {
				code = i;
				break;
			}
		}
		
		
		switch ( code ) {
			case REG_CMD:
				command = new REGCommand(parameter);
				return command;
			case LOGIN_CMD:
				command = new LOGINCommand(parameter);
				return command;
			case CONT_LIST_CMD:
				command = new CONT_LISTCommand(parameter);
				return command;
			case BLOCK_LIST_CMD:
				command = new BLOCK_LISTCommand(parameter);
				return command;
			case CHSTATUS_CMD:
				command = new CHSTATUSCommand(parameter);
				return command;
			case CHNICK_CMD:
				command = new CHNICKCommand(parameter);
				return command;
			case CHPSWD_CMD:
				command = new CHPSWDCommand(parameter);
				return command;
			case CHAVATAR_CMD:
				command = new CHAVATARCommand(parameter);
				return command;
			case CHMAIL_CMD:
				command = new CHMAIL_Command(parameter);
				return command;
			case LOGOUT_CMD:
				command = new LOGOUTCommand(parameter);
				return command;
			case UNREG_CMD:
				command = new UNREGCommand(parameter);
				return command;
			case DEL_CMD:
				command = new DELCommand(parameter);
				return command;
			case BLOCK_CMD:
				command = new BLOCKCommand(parameter);
				return command;
			case UNBLOCK_CMD:
				command = new UNBLOCKCommand(parameter);
				return command;
			case ADD_CMD:
				command = new ADD_USERCommand(parameter);
				return command;
			case FIND_CMD:
				command = new FINDCommand(parameter);
				return command;
			case ADD_MSGS_CMD:
				command = new ADD_MSGSCommand(parameter);
				return command;
			
		}
		
		return null;
	}
	
}
